import { TodoModel } from '../../model/Todo'


export interface DayVData {
  day:number,
  month:number,
  year:number,
  dayOfWeek:number,
  todos?:TodoModel[],
  isToday:boolean;
  isCurrentMonth:boolean;
  isSelected:boolean
}

export class DayVDataPartner {
  static OneDayMilliSecond: number = 60 * 60 * 24 * 1000

  private static  _today?:DayVData;
  static toMonthString(item:DayVData):string {
    return `${item.year}-${item.month}`
  }
  static toString(item:DayVData):string {
    return `${item.year}-${item.month}-${item.day}`
  }
  static today():DayVData {
    if(!DayVDataPartner._today) {
      let item = this.generateByDate(new Date());
      DayVDataPartner._today = item;
    }
    return DayVDataPartner._today;
  }

  static isTheSameDay(d1:DayVData,d2:DayVData):boolean {
    return d1.day == d2.day && d1.month == d2.month && d1.day == d2.day;
  }
  static isTheSameMonth(d1:DayVData,d2:DayVData) {
    return d1.day == d2.day && d1.month == d2.month;
  }


  static generateByDate(date:Date):DayVData {
    let dayItem = {} as DayVData;
    dayItem.year = date.getFullYear();
    dayItem.day = date.getDate();
    dayItem.month = date.getMonth() + 1;
    return dayItem;
  }

  static generate(year:number,month:number,day:number = 0):DayVData {
    let dayItem = {} as DayVData;
    dayItem.year = year;
    dayItem.day = day;
    dayItem.month = month;
    let today = DayVDataPartner.today();
    if(DayVDataPartner.isTheSameDay(dayItem,today)) {
      dayItem.isToday = true;
    }
    return dayItem;
  }
  static previousMonth(item:DayVData):DayVData {
    let dayItem = {} as DayVData;
    if(item.month == 1) {
      dayItem.year = item.year - 1;
      dayItem.month = 12;
    }
    else {
      dayItem.year = item.year;
      dayItem.month = item.month - 1;
    }
    return dayItem;
  }
  //获取一个月中的某一号,如果没有这个号,返回这个月最后一天.
  static getSelDay(day:DayVData,dayOfMonth:number):DayVData {
    let dayCount = DayVDataPartner.getThisMonthDayCount(day.year,day.month);
    if(dayOfMonth > dayCount) {
      return DayVDataPartner.generate(day.year,day.month,dayCount);
    }
    else {
      return DayVDataPartner.generate(day.year,day.month,dayOfMonth);
    }
  }
  static nextMonth(item:DayVData):DayVData {
    let dayItem = {} as DayVData;
    if(item.month == 12) {
      dayItem.year = item.year + 1;
      dayItem.month = 1;
    }
    else {
      dayItem.year = item.year;
      dayItem.month = item.month + 1;
    }
    return dayItem;
  }

  /**
   * Date对象的day传0,然后调用getDate返回的是这个月的总天数.
   * @param year
   * @param month
   * @returns
   */
  static getThisMonthDayCount(year:number, month:number):number {
    return new Date(year, month, 0).getDate();
  }
}